<?php
namespace Sale\Model;

use Common\Model\CommonModel;

class CardSaleCancelModel extends CommonModel {


    public function get_list($where = array(),$order = '',$from =  0,$to = 0){
        $list = M("CardSaleCancel")->alias("s")->field("s.*,l.logistics_name,c.number AS card_number,c.vip_name, c.vip_phone, c.vip_plate_number,sale.sale_name,u.user_nicename,u.user_phone")->where($where)
            ->join("bdt_card c ON c.id = s.cid","LEFT")
            ->join("bdt_logistics l ON l.id = c.lid","LEFT")
            ->join("bdt_sale sale ON sale.id = s.sale_id","LEFT")
            ->join("bdt_users u ON u.id = s.uid","LEFT")
            ->order($order)
            ->limit($from , $to)
            ->select();
        return $list;
    }

    /**
     * 新增会员卡消费记录
     */
    public function insert_data($card_number,$good_num,$desc,$sale_good){
        $sale = get_sale_data();
        $admin_card_sale_point = get_config_by_key('admin_card_sale_point');
        $logistics_card_sale_point = get_config_by_key('logistics_card_sale_point');
        $admin_oil_discount = get_config_by_key('admin_oil_discount');




        /*卡号不存在则返回失败*/
        $card = M("Card")->where(array("number"=>$card_number))->find();
        if(empty($card)){
            return -1;
        }
        if($card["status"] != 1){
            return -4;
        }

        $goods_price = round($sale_good['good_unit_price']*$good_num,2);
        $good_name = $sale_good['good_name'];
        $is_oil = $sale_good['is_oil'];

        /*如果是石油就计算优惠*/
        $disprice = 0;
        if ($is_oil == 1) {
            $disprice = $admin_oil_discount * $good_num;
        }

        $price = $goods_price - $disprice;

        //查询该消费机构的上级消费机构
        if($sale["sale_manager_id"]>0){
            $sale_manager = M("SaleManager")->where(array('id'=>$sale["sale_manager_id"]))->find();
            if($sale_manager["balance"]<$price){
                return -3;
            }
        }

        /*扣款余额不足则返回失败*/
        if($card['balance'] < $price){
            return -2;
        }

        /*计算*/
        // 应得积分
        $point = ceil($price/100*$admin_card_sale_point)+ceil($price/100*$logistics_card_sale_point);
        // 总积分
        $general_point = ceil($price/100*$admin_card_sale_point);
        // 自营积分
        $self_point = ceil($price/100*$logistics_card_sale_point);
        // 卡内余额
        $plus_price = round($card['balance']-$price,2);
        // 卡内积分
        $plus_point = $card['use_point']+$point;
        $card_point = $card['point']+$point;
        // 消费时间
        $create_time = time();
        // 操作者
        $uid = session('ADMIN_ID');
        // 订单号
        $number = date("YmdHis").rand(10000,99999);

        /*插入数组*/
        $insert = array();
        
        $insert['price'] = $price;
        $insert['dis_price'] = $disprice;
        $insert['admin_oil_discount'] = $admin_oil_discount;
        $insert['goods_num'] = $good_num;
        $insert['goods_price'] = $goods_price;
        $insert['desc'] = $desc;
        $insert['uid'] = $uid;

        $insert['cid'] = $card['id'];
        $insert['card_number'] = $card['number'];

        $insert['plus_price'] = $plus_price;

        $insert['general_point'] = $general_point;
        $insert['self_point'] = $self_point;
        $insert['plus_point'] = $plus_point;
        
        $insert['goods_name'] = $sale_good['good_name'];
        $insert['goods_unit_price'] = $sale_good['good_unit_price'];
        $insert['goods_unit'] = $sale_good['goods_unit'];
        $insert['sale_id'] = $sale['id'];
        $insert["sale_price"] = $sale["price"];
        $insert['lid'] = $card['lid'];
        $insert['is_oil'] = $is_oil;

        $insert['number'] = $number;
        $insert['create_time'] = $create_time;
        $insert["sale_manager_id"] = $sale["sale_manager_id"];
        $result = $this->add($insert);
        if($result){    //消费成功 扣除卡金额
            M("Card")->where(array("id"=>$card['id']))->save(
                array(
                    "balance"=>$plus_price,
                    "point"=>$card_point,
                    "use_point"=>$plus_point,
                    'last_sale_time'=>$create_time
                )
            );
            M("Sale")->where(array("id"=>$sale['id']))->save(array("price"=>$sale['price']+$price));
            if($sale["sale_manager_id"]>0){
                M("SaleManager")->where(array('id'=>$sale["sale_manager_id"]))->setDec('balance',$price);
            }
        }
        return $result;
    }
}